import tkinter as tk
import random

# 创建主窗口
root = tk.Tk()
root.title("逐行查找")

# 创建标签用于显示结果
result_label = tk.Label(root, text="")
result_label.pack(pady=20)

# 要搜索的答案
str_to_search = 10000
# 随机生成一个较大的二维数组
size = 100000
data = [[] for _ in range(3)]  # X、Y、Z 分表
for n in range(len(data)):
    data[n] = [0] * size
for i in range(size):
    # 随机生成数字
    data[0][i] = random.randint(0, i)
    data[1][i] = random.randint(0, i)
    data[2][i] = random.randint(0, i)

# 普通查找，也叫线性查找
def search(d, s):
    res = -1
    if random.random() > 0.5:
        # 倒序
        for key in range(len(d)):
            if d[key] == s:
                res = key
                break
    else:
        # 正序
        for key in range(len(d) - 1, -1, -1):
            if d[key] == s:
                res = key
                break
    return res

# 分表查找
html_text = "没有找到答案"
for n in range(len(data)):
    table = data[n]
    res = search(table, str_to_search)
    if res > -1:
        html_text = f"{str_to_search}，在表 {n + 1} 的第 {res + 1} 行"
        break

# 显示查找结果
result_label.config(text=html_text)
print(html_text)

# 数组
arr = ['20', '1', '21', '11']
# 正则表达式
import re
preg = re.compile(r'^1+')
for index, item in enumerate(arr):
    if preg.match(item):
        print(f'找到了1开头的记录 {item} {index}')

# 运行主循环
root.mainloop()